package de.hbrs.ooka.dhs.persistence;

import java.util.List;

import javax.persistence.Query;

import de.hbrs.ooka.dhs.entities.DHSComponentEntity;

public class DHSComponentEntityDatabaseManager extends DatabaseManager<DHSComponentEntity>{

	public DHSComponentEntityDatabaseManager() {
		super(DHSComponentEntity.class);
	}
	
	public List<DHSComponentEntity> findAllRunningOrderedByPosition() {
		Query query = getEntityManager().createQuery("Select d from DHSComponentEntity d where d.running=true order by d.position", DHSComponentEntity.class);
		@SuppressWarnings("unchecked")
		List<DHSComponentEntity> dhsComponentEntities = query.getResultList();
		
		return dhsComponentEntities;
	}
	
	public DHSComponentEntity findByName(String name) {
		Query query = getEntityManager().createQuery("Select d from DHSComponentEntity d where d.name=:name", DHSComponentEntity.class);
		query.setParameter("name", name);
		DHSComponentEntity dhsComponentEntity = (DHSComponentEntity) query.getSingleResult();
		
		return dhsComponentEntity;
	}
	
	public int getMaxPosition(){
		Query query = getEntityManager().createQuery("Select max(d.position) from DHSComponentEntity d", DHSComponentEntity.class);
		int maxPosition = (int) query.getSingleResult();
		
		return maxPosition;
	}

}
